package com.lvy.hczn.front.business.controller;

import cn.hutool.core.util.StrUtil;
import com.lvy.hczn.front.business.dto.SearchCondition;
import com.lvy.hczn.front.business.util.ResultUtil;
import com.lvy.hczn.front.common.core.redis.RedisCache;
import com.lvy.hczn.front.common.exception.UtilException;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/wcs/redis/")
@RestController
@Slf4j
public class RedisController {

    @Autowired
    private RedisCache redisCache;

    /**
     * 分拣原料入库
     * 不同的入库展开不同的方法，流程写在服务端，不写在web端
     *
     * @param searchCondition
     * @return
     */
    @ApiOperation(value = "分拣原料入库", notes = "分拣原料入库")
    @PostMapping("delByKey")
    public ResultUtil sortingEntry(@RequestBody SearchCondition searchCondition) {
        try {
            if (StrUtil.isEmpty(searchCondition.getClientId())) {
                return ResultUtil.failed("用户信息为空");
            }
            if (StrUtil.isEmpty(searchCondition.getType())) {
                return ResultUtil.failed("类型为空");
            }
            redisCache.del(searchCondition.getType() + searchCondition.getClientId());
            return ResultUtil.ok();
        } catch (Exception e) {
            if (e instanceof UtilException) {
                return ResultUtil.failed(e.getMessage());
            } else {
                return ResultUtil.failed("系统异常");
            }
        }
    }

    /**
     * 根据map的key删除
     *
     * @param searchCondition
     * @return
     */
    @ApiOperation(value = "根据map的key删除", notes = "根据map的key删除")
    @PostMapping("delHashKey")
    public ResultUtil delHashKey(@RequestBody SearchCondition searchCondition) {
        try {
            if (StrUtil.isEmpty(searchCondition.getClientId())) {
                return ResultUtil.failed("用户信息为空");
            }
            if (StrUtil.isEmpty(searchCondition.getType())) {
                return ResultUtil.failed("类型为空");
            }
            if (StrUtil.isEmpty(searchCondition.getMapKey())) {
                return ResultUtil.failed("key为空");
            }
            redisCache.delHash(searchCondition.getType() + ":" + searchCondition.getClientId(), searchCondition.getMapKey());
            return ResultUtil.ok();
        } catch (Exception e) {
            if (e instanceof UtilException) {
                return ResultUtil.failed(e.getMessage());
            } else {
                return ResultUtil.failed("系统异常");
            }
        }
    }
}
